import { ProxyOb } from "./ProxyOb";
import VmBase from "./VmBase";

/*
 * @features: 主要功能
 * @description: 内容说明
 * @Date: 2021-09-11 16:15:03
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-09-11 17:15:31
 * @LastEditors: judu233
 */
const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('VM/VM-Sprite')
export default class VmSprite extends VmBase {

    @property({ type: [cc.SpriteFrame], tooltip: 'sprite将会用到帧图片' })
    spriteFrames: Array<cc.SpriteFrame> = [null];

    @property
    private _spriteIndex: number = 0;
    @property({ tooltip: '当前显示的spriteFrame', type: cc.Integer, min: 0, })
    get index() { return this._spriteIndex; }
    set index(value: number) {
        this._spriteIndex = cc.misc.clampf(parseInt(String(value)), 0, this.spriteFrames.length - 1);
        this.changeSprite();
    }

    sp: cc.Sprite;

    onValueInit() {
        this.sp = this.node.getComponent(cc.Sprite);
        super.onValueInit();
    }

    onValueChanged(n, o) {
        this.index = n;
    }
    
    /**设置 Sprite 组件的spriteFrame属性，变换图片     */
    changeSprite() {
        this.sp.spriteFrame = this.spriteFrames[this.index];
    }

    /**通过设置帧名字来设置对象 */
    setName(name: string) {
        let index = this.spriteFrames.findIndex(v => { return v.name == name });
        if (index < 0) { cc.error('frameIndex 设置了不存在的name:', name) }
        this.index = index || 0;
    }

    /**随机范围设置帧图片 */
    random(min = 0, max = 0) {
        if (!this.spriteFrames) return;
        min = cc.misc.clampf(min, 0, this.spriteFrames.length);
        max = cc.misc.clampf(max, 0, this.spriteFrames.length);
        this.index = Math.floor(Math.random() * (max - min) + min);
    }

    fast_next() { this.index++; }
    fast_previous() { this.index--; }
}
